package com.szy.assembly;

import java.io.*;
import java.util.ArrayList;


public class AsmTest implements IOChangedNotifier {
    public static void main(String[] args) {
        //System.out.println("123");
        try{
            InputStream fin=AsmTest.class.getClassLoader().getResourceAsStream("calltest.s");
            InputStreamReader isr=new InputStreamReader(fin);
            BufferedReader br=new BufferedReader(isr);
            String line="";
            StringBuilder code= new StringBuilder();
            while((line=br.readLine())!=null){
                code.append(line).append("\n");
            }
            //System.out.println(code);
            AsmTest test=new AsmTest();
            Assembler assembler=new Assembler(code.toString());
            Program program=null;
            if(assembler.assemble()){
                System.out.println("assemble successful!");
                program=assembler.getProgram();

                Peripheral gpioa=new GPIO(0x08000000).setIoChangedNotifier(test);
                ArrayList<Peripheral> peripherals=new ArrayList<>();
                peripherals.add(gpioa);

                CPUSimulator simulator=new CPUSimulator(program,256,peripherals);

                gpioa.ioChangedByHardware(1,0);
                simulator.restartPeriodically();
                try{
                    while(true){
                        simulator.cycleTask();
                        Thread.sleep(50);
                    }
                }catch (Exception e){}


            }else{
                System.out.println(assembler.getErrorList().get(0));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

//        ArrayList<String> l=new ArrayList<String>();
//        l.set(0,"aaa");
//        System.out.println(l.get(2));
//        System.out.println(l.get(5));
    }

    @Override
    public void ioChanged(int address, int value) {
        System.out.println("GPIO"+address+"->"+value);
    }
}
